<?php

class FP_Roles extends FP_Gateway {

    protected $_dao_class = 'FP_Dao_Role';

    protected $_model_class = 'FP_Role';

    protected $_primary_key = 'role_id';

//     public function fetchRoleByName($pRole){
//
// 	$results = $this->fetch(array('name' => $pRole));
//
// 	if (count($results) > 0)
// 	    return $results->current();
// 	return null;
//     }

    public function getParentRoles($pRole){
	$role = $this->getModelObject($pRole);

	$parents = array();

	if ($role->parent_id){

	    $parent_roles = $this->fetch(array('role_id' => $role->parent_id));

	    foreach ($parent_roles as $prole){
		$parents[] = $prole;

		$parents = array_merge(
		    $parents,
		    $this->getParentRoles($prole)
		);
	    }
	}

	return $parents;
    }

    public function getPermissions($pRole){
	$role_id = $this->getModelObjectId($pRole);

	$Permissions = FP_Application::classFactory('Permissions');
	return $Permissions->fetchPermissionsForRole($role_id);
    }

    public function addPermissionToRole($pRole, $pPermission){
	$role_id = $this->getModelObjectId($pRole);

	$Permissions = FP_Application::classFactory('Permissions');
	$Permissions->addPermissionToRole($role_id, $pPermission);

    }

    public function getModelObject($pRole){
	if (is_string($pRole) && !is_numeric($pRole))
	    return $this->fetchByName($pRole, true);
	return parent::getModelObject($pRole);

    }

    public function getModelObjectId($pRole){
	if (is_string($pRole) && !is_numeric($pRole)){
	    $role = $this->fetchByName($pRole, true);
	    return $role->role_id;
	}
	return parent::getModelObjectId($pRole);

    }
}

?>